//
//  MoreBtnForIphoneCell.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/18.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "MoreBtnForIphoneCell.h"

@implementation MoreBtnForIphoneCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self setUI];
    }
    return self;
}
- (void)setUI{
    self.titleLab = [[UILabel alloc]init];
    self.titleLab.textColor = Black_Color;
    self.titleLab.font = [UIFont systemFontOfSize:14*ZitiRate];
    [self.contentView addSubview:self.titleLab];
    [self.titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(10*WidthRate);
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(45*WidthRate);
        make.width.mas_equalTo(80*WidthRate);
    }];
    
   
    self.contentLab = [[UILabel alloc]init];
    self.contentLab.textColor = Gray_Color;
    self.contentLab.font = [UIFont systemFontOfSize:12*ZitiRate];
    self.contentLab.textAlignment = 2;
    self.contentLab.numberOfLines = 0;
    [self.contentView addSubview:self.contentLab];
    [self.contentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.height.mas_equalTo(45*WidthRate);
        make.left.equalTo(self.titleLab.mas_right).offset(0);
        make.right.mas_equalTo(-20*WidthRate);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = Line_Color;
    [self.contentView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(45*WidthRate);
        make.height.mas_equalTo(1);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    NSArray *safeList = @[@"施工标志摆放正确",@"施工人员着标志服、标志帽",@"施工程序符合安全作业规程",@"施工车辆安全行驶",@"危险段施工及危险工作保护措施",@"施工现场有安全管理员"];
    for (int i=0; i<6; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.layer.cornerRadius = 5;
        btn.layer.masksToBounds = YES;
        btn.titleLabel.textAlignment = 1;
        btn.titleLabel.numberOfLines = 2;
        btn.titleLabel.font = [UIFont systemFontOfSize:10*ZitiRate];
        [btn setTitle:safeList[i] forState:UIControlStateNormal];
        btn.tag = 100+i;
        btn.backgroundColor = [PublicMethod colorWithHexString:@"#dadada" andAlpha:1];
        [btn setTitleColor:Black_Color forState:UIControlStateNormal];
        btn.frame = CGRectMake(15*WidthRate+(180*WidthRate*(i%2)), 55*WidthRate+(38*WidthRate*(i/2)), 165*WidthRate, 28*WidthRate);
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
        
        
    }
    
}
- (void)addValue:(NSArray *)arr{
    for (int i=0; i<arr.count; i++) {
        if ([arr[i] isEqualToString:@"0"]) {
            UIButton *btn = (UIButton *)[self.contentView viewWithTag:100+i];
            btn.selected = NO;
            btn.backgroundColor = [PublicMethod colorWithHexString:@"#dadada" andAlpha:1];
            [btn setTitleColor:Black_Color forState:UIControlStateNormal];
        }else{
            UIButton *btn = (UIButton *)[self.contentView viewWithTag:100+i];
            btn.selected = YES;
            btn.backgroundColor = MAIN_Color;
            [btn setTitleColor:Bai_Color forState:UIControlStateNormal];
        }
    }
}
- (void)btnClick:(UIButton *)btn{
    btn.selected = !btn.selected;
    if (btn.selected) {
        btn.backgroundColor = MAIN_Color;
        [btn setTitleColor:Bai_Color forState:UIControlStateNormal];
    }else{
        btn.backgroundColor = [PublicMethod colorWithHexString:@"#dadada" andAlpha:1];
        [btn setTitleColor:Black_Color forState:UIControlStateNormal];
    }
    
    [self changeArray];
}
- (void)changeArray{
    NSMutableArray *arr = [[NSMutableArray alloc]init];
    for (int i =0 ; i<6; i++) {
        UIButton *btn = (UIButton *)[self.contentView viewWithTag:100+i];
        NSString *str = [NSString stringWithFormat:@"%d",btn.selected];
        [arr addObject:str];
    }
    if ([self.delegate respondsToSelector:@selector(moreBtnForIPhoneCellWithArray:)]) {
        [self.delegate moreBtnForIPhoneCellWithArray:arr];
    }
}
- (void)setState:(NSInteger)state{
    _state = state;
    if (state==1) {
        for (int i = 0; i<6; i++) {
            UIButton *btn = (UIButton *)[self.contentView viewWithTag:100+i];
            btn.userInteractionEnabled = NO;
        }
    }else{
        for (int i = 0; i<6; i++) {
            UIButton *btn = (UIButton *)[self.contentView viewWithTag:100+i];
            btn.userInteractionEnabled = YES;
        }
    }
}
@end
